package ru.ssau.university.web.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import ru.ssau.university.ejb.api.UniversityServiceLocal;
import ru.ssau.university.persistence.entity.Group;
import ru.ssau.university.persistence.entity.PlanItem;
import ru.ssau.university.persistence.entity.Schedule;
import ru.ssau.university.persistence.entity.Speciality;
import ru.ssau.university.persistence.entity.Subject;
import ru.ssau.university.web.helper.ScheduleRow;
import ru.ssau.university.web.helper.ScheduleWrapper;

@ManagedBean(name = "scheduleBean")
@SessionScoped
public class ScheduleBean {
	@EJB
	private UniversityServiceLocal universityService;

	private Long groupId;
	private Group group;
	private List<ScheduleRow> evenWeek;
	private List<ScheduleRow> oddWeek;
	private List<PlanItem> plan;
	private boolean editMode;

	public String initAll() {
		group = universityService.getGroupById(groupId);
		loadPlan();
		prepareEvenWeek();
		prepareOddWeek();
		return null;
	}

	private void loadPlan() {
		int term = group.getTerm();
		Speciality speciality = group.getSpeciality();
		plan = universityService.getPlanItems(speciality.getId(), term);
	}

	private List<Subject> getSubjects() {
		List<Subject> res = new ArrayList<Subject>();
		for (PlanItem pi : plan) {
			res.add(pi.getSubject());
		}
		return res;
	}

	private void prepareEvenWeek() {
		evenWeek = new ArrayList<ScheduleRow>();
		List<Subject> subjects = getSubjects();
		for (int lessonNum = 0; lessonNum < 7; lessonNum++) {
			ScheduleRow sr = new ScheduleRow(lessonNum, universityService,
					groupId, 0, subjects);
			evenWeek.add(sr);
		}
	}

	private void prepareOddWeek() {
		group = universityService.getGroupById(groupId);
		oddWeek = new ArrayList<ScheduleRow>();
		List<Subject> subjects = getSubjects();
		for (int lessonNum = 0; lessonNum < 7; lessonNum++) {
			ScheduleRow sr = new ScheduleRow(lessonNum, universityService,
					groupId, 1, subjects);
			oddWeek.add(sr);
		}
	}

	public String finishEdit() {
		editMode = false;
		return null;
	}

	public String edit() {
		editMode = true;
		return null;
	}

	public String save() {
		saveWeek(evenWeek);
		saveWeek(oddWeek);
		initAll();
		return null;
	}

	private void saveWeek(List<ScheduleRow> week) {
		Subject nothingSubject = new Subject();
		nothingSubject.setTitle("---");
		for (ScheduleRow sr : week) {
			for (ScheduleWrapper sw : sr.getItems()) {
				Subject oldSubject = null;
				Schedule schedule = sw.getSchedule();
				if (schedule != null) {
					oldSubject = schedule.getPlanItem().getSubject();
				}
				Subject newSubject = sw.getChoosedSubject();

				boolean b1 = (oldSubject == null && !nothingSubject
						.equals(newSubject));
				if (b1 || (!newSubject.equals(oldSubject))
						&& !newSubject.equals(nothingSubject)) {
					int lesson = week.indexOf(sr);
					int dayOfWeek = sr.getItems().indexOf(sw);
					saveOrUpdateSchedule(schedule, newSubject, lesson,
							sr.getWeek(), dayOfWeek);
				}

				if (schedule != null && nothingSubject.equals(newSubject)) {
					deleteSchedule(schedule);
				}
			}
		}
	}

	private void deleteSchedule(Schedule schedule) {
		universityService.deleteSchedule(schedule);
	}

	private void saveOrUpdateSchedule(Schedule schedule, Subject subject,
			int lesson, int week, int dayOfWeek) {
		if (schedule == null) {
			schedule = new Schedule();
			schedule.setGroup(group);
			schedule.setLesson(lesson);
			schedule.setDayOfWeek(dayOfWeek);
			schedule.setWeek(week);
		}
		for (PlanItem pi : plan) {
			if (pi.getSubject().getId().equals(subject.getId())) {
				System.out.println("true");
				schedule.setPlanItem(pi);
				break;
			}
			System.out.println("plan.size: " + plan.size());
			for (PlanItem p : plan) {
				System.out.println("plan.subject: " + p.getSubject());
				System.out
						.println("plan.subject_id: " + p.getSubject().getId());
			}
			System.out.println("!!! " + subject.getTitle());
			System.out.println("!!! " + subject.getId());
		}
		universityService.saveOrUpdateSchedule(schedule);
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}

	public List<ScheduleRow> getEvenWeek() {
		return evenWeek;
	}

	public void setEvenWeek(List<ScheduleRow> evenWeek) {
		this.evenWeek = evenWeek;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public List<ScheduleRow> getOddWeek() {
		return oddWeek;
	}

	public void setOddWeek(List<ScheduleRow> oddWeek) {
		this.oddWeek = oddWeek;
	}

	public List<PlanItem> getPlan() {
		return plan;
	}

	public void setPlan(List<PlanItem> plan) {
		this.plan = plan;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}
}
